﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace EffiProz.Core.Lib
{
    /**
 *
 * New Class based on original Hypersonic code.
 *
 * @author Thomas Mueller (Hypersonic SQL Group)
 * @version 1.7.2
 * @since Hypersonic SQL
 */
public class ZipUnzipFile {

    private static int COPY_BLOCK_SIZE = 1 << 16;

    public static void compressFile(string infilename, string outfilename,
                                    EfzFileAccess storage) {

        Stream       _in        = null;
        GZipStream   f         = null;
        bool      completed = false;

        // if there is no file
        if (!storage.IsStreamElement(infilename)) {
            return;
        }

        try {
            byte[] b = new byte[COPY_BLOCK_SIZE];

            _in = storage.OpenInputStreamElement(infilename);
            f = new GZipStream(
                storage.OpenOutputStreamElement(outfilename),
                CompressionMode.Compress);

            while (true) {
                int l = _in.Read(b, 0, COPY_BLOCK_SIZE);

                if (l == 0) {
                    break;
                }

                f.Write(b, 0, l);
            }

            completed = true;
        } catch (Exception e) {
            throw FileUtil.toIOException(e);
        } finally {
            try {
                if (_in != null) {
                    _in.Close();
                }

                if (f != null) {
                    f.Flush();    // reported to be missing from close() in some JRE libs
                    f.Close();
                }

                if (!completed && storage.IsStreamElement(outfilename)) {
                    storage.RemoveElement(outfilename);
                }
            } catch (Exception e) {
                throw FileUtil.toIOException(e);
            }
        }
    }

    public static void decompressFile(string infilename, String outfilename,
                                      EfzFileAccess storage)  {

        GZipStream f = null;
        Stream        outstream = null;
        bool            completed = false;

        try {
            if (!storage.IsStreamElement(infilename))
            {
                return;
            }

            storage.RemoveElement(outfilename);

            f = new GZipStream(
                storage.OpenInputStreamElement(infilename), CompressionMode.Decompress);
            outstream = storage.OpenOutputStreamElement(outfilename);

            byte[] b = new byte[COPY_BLOCK_SIZE];

            while (true) {
                int l = f.Read(b, 0, COPY_BLOCK_SIZE);

                if (l == 0) {
                    break;
                }

                outstream.Write(b, 0, l);
            }

            completed = true;
        } catch (Exception e) {
            throw FileUtil.toIOException(e);
        } finally {
            try {
                if (f != null) {
                    f.Close();
                }

                if (outstream != null) {
                    outstream.Flush();
                    outstream.Close();
                }

                if (!completed && storage.IsStreamElement(outfilename)) {
                    storage.RemoveElement(outfilename);
                }
            } catch (Exception e) {
                throw FileUtil.toIOException(e);
            }
        }
    }
}
}
